<template>
  <div class="container">
    <!-- 此页面用于展示点击时间后，传入参数展示对应类别的课程卡片 -->
    <div class="course-box">
      <div class="module-title">全部课程</div>
      <div class="NavHeader">
        <div class="b_hjr">
          <div class="Sdujj">
            <!-- 首个全部对应的点击事件不一样 -->
            <span class="_3dFLK _3bx0D" @click="getAll($event)"
              >全部（91）</span
            >
            <!-- 通用筛选点击事件 -->
            <span class="_3dFLK" @click="getSelectStage(1, $event)"
              >正在进行</span
            >
            <span class="_3dFLK" @click="getSelectStage(0, $event)"
              >即将开始</span
            >
            <span class="_3dFLK" @click="getSelectStage(-1, $event)"
              >已结束</span
            >
          </div>
          <div class="-PXk9">
            <span class="_3dFLK">综合</span>
            <span class="_3dFLK">热门</span>
            <span class="_3dFLK _3bx0D">最新</span>
          </div>
        </div>
      </div>
      <div class="courseList">
         <el-empty class="empty"  v-if="courseTotal==0" description="暂无课程" />
        <div
        v-if="courseTotal"
          class="course-item"
          v-for="(item, index) in courseList"
          :key="index"
        >
          <!-- 封面 -->
          <div
            class="course-pic"
            :style="{
              height: 126 + 'px',
              background:
                'url(' + item.coursePicUrl + ')  0px 50% / cover no-repeat',
            }"
          ></div>
          <div class="details">
            <div class="description">
              <!-- 课程名 -->
              <h3 :title="item.courseName">
                {{ item.courseName }}
              </h3>
              <!-- 出品机构 -->
              <p>{{ item.courseFrom }}</p>
              <!-- 授课人 -->
              <div class="_1Zkj9">{{ item.courseInstructor }}</div>
            </div>
            <div class="condition">
              <span class="people"> 123人参加 </span>
              <div v-if="item.expense === 0" class="expense">
                <img src="../../assets/images/dollar.png" />
                免费
              </div>
              <div v-if="item.expense !== 0" class="expense">
                <img src="../../assets/images/dollar.png" />
                {{ item.expense }}
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="page">
        <!-- 底部分页 -->
        <div class="homePage" v-on:click="homePageFn">首页</div>
        <el-pagination
          v-model:currentPage="currentPage"
          v-model:page-size="pageSize"
          background
          prev-text="上一页"
          next-text="下一页"
          layout="prev, pager, next"
          :total="courseTotal"
        />
        <div class="lastPage" v-on:click="lastPageFn">尾页</div>
      </div>
    </div>
  </div>
</template>
<script>
import { useRoute } from "vue-router";
import { courseTotal, courseList } from "../../api/course.js";
import { login } from "../../api/user.js";
export default {
  data() {
    return {
      category: "计算机科学",
      courseList: [
        {
          courseId: "7758258",
          coursePicUrl:
            "https://jack-chen.oss-cn-chengdu.aliyuncs.com/%E5%9B%BE%E7%89%87/coursepic1.webp",
          courseName: "Python科学计算三维可视化",
          courseFrom: "北京理工大学",
          courseInstructor: "黄天羽",
          expense: 0,
          currentPeople: 123,
        },
        {
          courseId: "7758258",
          coursePicUrl:
            "https://jack-chen.oss-cn-chengdu.aliyuncs.com/%E5%9B%BE%E7%89%87/coursepic1.webp",
          courseName: "Python科学计算三维可视化",
          courseFrom: "北京理工大学",
          courseInstructor: "黄天羽",
          expense: 0,
          currentPeople: 123,
        },
        {
          courseId: "7758258",
          coursePicUrl:
            "https://jack-chen.oss-cn-chengdu.aliyuncs.com/%E5%9B%BE%E7%89%87/coursepic1.webp",
          courseName: "Python科学计算三维可视化",
          courseFrom: "北京理工大学",
          courseInstructor: "黄天羽",
          expense: 0,
          currentPeople: 123,
        },
        {
          courseId: "7758258",
          coursePicUrl:
            "https://jack-chen.oss-cn-chengdu.aliyuncs.com/%E5%9B%BE%E7%89%87/coursepic1.webp",
          courseName: "Python科学计算三维可视化",
          courseFrom: "北京理工大学",
          courseInstructor: "黄天羽",
          expense: 0,
          currentPeople: 123,
        },
        {
          courseId: "7758258",
          coursePicUrl:
            "https://jack-chen.oss-cn-chengdu.aliyuncs.com/%E5%9B%BE%E7%89%87/coursepic1.webp",
          courseName: "Python科学计算三维可视化",
          courseFrom: "北京理工大学",
          courseInstructor: "黄天羽",
          expense: 0,
          currentPeople: 123,
        },
        {
          courseId: "7758258",
          coursePicUrl:
            "https://jack-chen.oss-cn-chengdu.aliyuncs.com/%E5%9B%BE%E7%89%87/coursepic1.webp",
          courseName: "Python科学计算三维可视化",
          courseFrom: "北京理工大学",
          courseInstructor: "黄天羽",
          expense: 0,
          currentPeople: 123,
        },
        {
          courseId: "7758258",
          coursePicUrl:
            "https://jack-chen.oss-cn-chengdu.aliyuncs.com/%E5%9B%BE%E7%89%87/coursepic1.webp",
          courseName: "Python科学计算三维可视化",
          courseFrom: "北京理工大学",
          courseInstructor: "黄天羽",
          expense: 0,
          currentPeople: 123,
        },
        {
          courseId: "7758258",
          coursePicUrl:
            "https://jack-chen.oss-cn-chengdu.aliyuncs.com/%E5%9B%BE%E7%89%87/coursepic1.webp",
          courseName: "Python科学计算三维可视化",
          courseFrom: "北京理工大学",
          courseInstructor: "黄天羽",
          expense: 0,
          currentPeople: 123,
        },
        {
          courseId: "7758258",
          coursePicUrl:
            "https://jack-chen.oss-cn-chengdu.aliyuncs.com/%E5%9B%BE%E7%89%87/coursepic1.webp",
          courseName: "Python科学计算三维可视化",
          courseFrom: "北京理工大学",
          courseInstructor: "黄天羽",
          expense: 0,
          currentPeople: 123,
        },
      ],
      // 修改项
      courseTotal: 90,
      currentPage: 1,
      pageSize: 20,
    };
  },
  // 1、在合适的生命周期解析获取到课程类别参数
  created() {
    // 获取类别
    this.categoryName = this.getParams();
    //获取这个类别的课程数量
    this.courseTotal = this.getCourseTotal();
    //根据类别获取课程
    this.getCourseList()
  },
  mounted() {},
  // 采用自定义监听器来相应数据变化后的操作
  watch: {
    currentPage(newPage) {},
  },
  methods: {
    getParams() {
      const route = useRoute();
      let categoryName = route.query.categoryName;
      return categoryName;
    },

    getCourseTotal() {
      courseTotal(this.category).then((res) => {
        console.log(res);
        // 总数需要赋值刷新
      });
    },
    getCourseList() {
      courseList(this.category, this.currentPage, this.pageSize).then((res) => {
        // 修改获取list
        console.log(res.data)
      });
    },
    // 分页器 首页尾页，直接的点击跳转
    homePageFn() {
      if (this.currentPage == 1) {
        return;
      }
      this.currentPage = 1;
      //  调取列表数据
      this.getCourseList();
    },
    lastPageFn() {
      var lastPage = Math.ceil(this.courseTotal / this.pageSize);
      if (this.currentPage == lastPage) {
        return;
      }
      this.currentPage = lastPage;
      //调取列表数据
      this.getCourseList();
    },
    // 适用于在DOM节点上进行的样式类操作
    getAll(event) {
      if (event.currentTarget.classList.contains("_3bx0D")) {
        return;
      } else {
        event.currentTarget.classList.add("_3bx0D");
        let currentNode = event.currentTarget;
        let currentBroNodes = event.currentTarget.parentNode.children;
        let len = currentBroNodes.length;
        for (var i = 0; i < len; i++) {
          if (currentBroNodes[i] !== currentNode) {
            if (currentBroNodes[i].classList.contains("_3bx0D")) {
              currentBroNodes[i].classList.remove("_3bx0D");
            }
          }
        }
        // 请求数据，包括课程数据还有对应阶段的课程总数
        // 自定义过滤器



      }

    },
    getSelectStage(stage, event) {
      if (event.currentTarget.classList.contains("_3bx0D")) {
  
       return;
      } else {
        event.currentTarget.classList.add("_3bx0D");
        let currentNode = event.currentTarget;
        let currentBroNodes = event.currentTarget.parentNode.children;
        let len = currentBroNodes.length;
        for (var i = 0; i < len; i++) {
          if (currentBroNodes[i] !== currentNode) {
            if (currentBroNodes[i].classList.contains("_3bx0D")) {
              currentBroNodes[i].classList.remove("_3bx0D");
            }
          }
        }
        // 数据操作


      }
    },
  },
};
</script>

<style scoped>
.container {
  height: 100%;
  background-color: rgb(245, 245, 245);
}
.course-box {
  width: 80%;
  height: 100%;
  margin-left: 10%;
}
.module-title {
  margin-top: 0;
  margin-left: 0;
  height: 50px;
  width: 100px;
  font-size: 20px;
  text-align: center;
  line-height: 50px;
  font-weight: bolder;
  margin-left: 20px;
}
.NavHeader {
  background: transparent;
}
.b_hjr {
  width: 1400px;
  overflow: hidden;
}
.Sdujj {
  float: left;
  overflow: hidden;
  margin-left: 40px;
}
._3dFLK._3bx0D {
  color: #00cc7e;
}
._3dFLK {
  cursor: pointer;
  display: inline-block;
  height: 17px;
  line-height: 17px;
  font-size: 12px;
  margin: 0 20px 12px 0;
  color: #666;
}
._3dFLK:hover {
  color: #00cc7e;
}
.-PXk9 {
  float: right;
  overflow: hidden;
  margin-right: 140px;
}
.NavHeader span {
  color: #666;
  display: inline-block;
  font-size: 14px;
  cursor: pointer;
  margin-right: 32px;
}
.courseList {
  min-height: 200px;
  overflow: hidden;
  padding-top: 10px;
  display: flex;
  flex-wrap: wrap;
}
.course-item {
  overflow: hidden;
  cursor: pointer;
  position: relative;
  display: inline-block;
  width: 224px;
  height: 252px;
  border-radius: 8px;
  background: #fff;
  margin-left: 30px;
  margin-bottom: 20px;
}
.course-pic {
  height: 126px;
}
.course-item .details {
  position: relative;
  padding: 10px 12px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  height: 126px;
  border-radius: 0 0 8px 8px;
}

.details .description h3 {
  text-align: left;
  max-height: 36px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
  line-height: 18px;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 0;
}
.details .description p {
  text-align: left;
  height: 12px;
  line-height: 12px;
  font-size: 12px;
  color: #666;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;

  margin-top: -5px;
}
._1Zkj9 {
  text-align: left;
  width: 100%;
  height: 17px;
  line-height: 17px;
  margin: 8px 0 50 0;
  font-size: 12px;
  color: #999;
  overflow: hidden;
  -o-text-overflow: ellipsis;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.details .condition {
  height: 12px;
  line-height: 12px;
  font-size: 12px;
  color: #999;
  overflow: hidden;
  position: absolute;
  bottom: 11px;
  width: calc(100% - 24px);
}
.people {
  float: right;
  display: flex;
  flex-flow: row nowrap;
  justify-content: center;
  text-align: right;
}
.expense {
  color: #53b880;
  float: left;
}
/* 分页管理器样式 后面可以自定义好看的样式 */
.page {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 13px;
  color: #656565;
  margin-top: 20px;
  padding-bottom: 20px;
}

.page > div.homePage,
.page > div.lastPage {
  height: 34px;
  line-height: 34px;
  width: 50px;
  text-align: center;
  border: 1px solid #eaeaea;
  box-sizing: border-box;
  cursor: pointer;
}

.page > div.homePage {
  border-right: none;
  border-radius: 2px 0 0 2px;
}

.page > div.lastPage {
  border-left: none;
  border-radius: 0 2px 2px 0;
}
.el-pagination {
  text-align: center;
  padding: 0;
}
/* 空状态设置 */
.empty{
  margin: 0 auto;
}
</style>